const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const md5 = require('./md5')

function api_request(url, method, parm){
    
  var apiurl    = 'https://pbootcms.symbol365.com/api.php?p=';  //域名 + api.php
  // var apiurl    = 'http://shanghui.dev.lishengli.xin/api.php?p=';  //域名 + api.php
  var appid     = 'wx4f62a4703c0d229b';    //后台WebAPI中设置的API认证用户
  var appsecret = 'f5956b77cf0e2e03aa967df7c9461895';    //后台WebAPI中设置的API认证密钥
  
  var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;    //获取当前时间戳
    
  var signature = md5.hexMD5(md5.hexMD5(appid + appsecret + timestamp));    //构建signature

  var data = {    //因为搜索、留言和表单需要用到POST方法，要通过data传递参数，所以先声明data对象，方便后面添加
     appid: appid,
     timestamp: timestamp,
     signature: signature,
  };

  //根据请求方法构建Header
  if (method == 'POST') {
    //POST方法，设置请求头部
    var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };

    //添加POST参数到data
    for (var i in parm) {
       data[i] = parm[i];
    }
  } else {
    //GET方法，设置请求头部
    method = 'GET';
    var header = { 'Content-Type': 'application/json' };
  }

  //通过promise返回数据
  var promise = new Promise(function(resolve,reject){
        console.log(data)
    //小程序自带的wx.request请求
    wx.request({

      method: method,
      url: apiurl + url,
      header: header,
      data: data,
            
      //请求成功
      success: function(res){
                
        var api_data = res.data;

        if (api_data.code != 1 && api_data.code != 0 && api_data.code != 2){

          reject({ error: '数据请求失败，请稍后重试', code: 500 });
          return;

        } else {

          resolve(api_data);
        
        }
      
      } 

    })

  })
   
    //返回数据
  return promise;
}

module.exports = {
  formatTime,
  //获取公司信息
  companyinfos: function(){
    var url = '/cms/company';
    return api_request(url);
  },

  //获取单个公司信息
  companyinfo: function(name){
    var url = '/cms/company/name/' + name;
    return api_request(url);
  },

  //获取网站信息
  siteinfos: function(){
    var url = '/cms/site';
    return api_request(url);
  },

  //获取单个网站信息
  siteinfo: function(name){
    var url = '/cms/site/name/' + name;
    return api_request(name);
  },

  //获取自定义标签
  get_labels: function(){
    var url = '/cms/label';
    return api_request(url);
  },

  //获取单个自定义标签
  get_label: function(){
    var url = '/cms/label/name/' + name;
    return api_request(url);
  },

  //获取幻灯片
  get_slide: function(gid,num){
    var url = '/cms/slide/gid/' + gid + '/num/' + num;
    return api_request(url);
  },

  //获取友情链接
  get_links: function(gid,num){
    var url = '/cms/link/gid/' + gid + '/num/' + num;
    return api_request(url);
  },

  //获取栏目列表
  get_navs: function(){
    var url = '/cms/nav';
    return api_request(url);
  },

  //获取指定栏目列表
  get_nav: function(scode){
    var url = '/cms/nav/scode/' + scode;
    return api_request(url);
  },

  //获取当前栏目
  get_current_cat: function(scode){
    var url = '/cms/sort/scode/' + scode;
    return api_request(url);
  },

  //获取内容列表
  get_list: function(scode,num,order){
    if( ( num != '' ) && !order ){
      var url = '/list/' + scode + '/num/' + num;
    } else if( !num && ( order != '' ) ){
      var url = '/list/' + scode + '/order/' + order;
    } else if ( (num != '') && (order != '') ){
      var url = '/list/' + scode + '/num/' + num + '/order/' + order;
    } else {
      var url = '/list/' + scode;
    }
    return api_request(url);
  },

  //获取内容列表第*页
  get_list_paged: function (scode, paged, num, order) {
    if ((num != '') && (order == '')) {
      var url = '/list/' + scode + '/num/' + num + '/page/' + paged;
    } else if ((num == '') && (order != '')) {
      var url = '/list/' + scode + '/order/' + order + '/page/' + paged;
    } else if ((num != '') && (order != '')) {
      var url = '/list/' + scode + '/num/' + num + '/order/' + order + '/page/' + paged;
    } else {
      var url = '/list/' + scode + '/page/' + paged;
    }
    return api_request(url);
  },

  //获取指定内容
  get_content: function(id){
    var url = '/content/' + id;
    return api_request(url);
  },

  //获取指定内容多图
  get_content_pics: function(id){
    var url = '/cms/pics/id/' + id;
    return api_request(url);
  },

  //获取搜索结果
  get_search_resault: function(parm,num,order){ 
    if ((num != '') && (order == '')) {
      var url = '/cms/search/' + 'num/' + num;
    } else if ((num == '') && (order != '')) {
      var url = '/cms/search/' + 'oder/' + order;
    } else if ((num != '') && (order != '')) {
      var url = '/cms/search/num/' + num + '/order/' + order;
    } else {
      var url = '/cms/search/';
    }
    return api_request(url, 'POST', parm);
  },

  //提交留言
  add_message: function(parm){
    var url = '/cms/addmsg';
    return api_request(url, 'POST', parm);
  },

  //获取留言内容
  get_message: function(num){
    var url = '/cms/msg/num/' + num;
    return api_request(url);
  },

  //获取留言内容第*页
  get_message_paged: function(num,paged){
    var url = '/cms/msg/num/' + num + '/page/' + paged;
    return api_request(url);
  },

  //提交表单
  add_form: function(fcode,parm){
    var url = '/cms/addform/fcode/' + fcode;
    return api_request(url, 'POST', parm);
  },

  //获取表单内容
  get_form: function(fcode){
    var url = '/cms/form/fcode/' + fcode;
    return api_request(url);
  },

  //获取表单内容第*页
  get_form_paged: function(fcode,num,paged){
    var url = '/cms/form/fcode/' + fcode + '/num/' + num + '/page/' + paged;
    return api_request(url);
  },
  //添加企业职员
  add_enterprise_member: function(parm){
    var url = '/enterpriseMember/add';
    return api_request(url, 'POST', parm);
  },
  //编辑企业职员
  edit_enterprise_member: function(parm){
    var url = '/enterpriseMember/edit';
    return api_request(url, 'POST', parm);
  },
  del_enterprise_member: function(param){
    var url = '/enterpriseMember/delMember';
    return api_request(url, 'POST', param);
  },
  //查询企业职员列表
  get_member_list: function(param){
     var url = '/enterpriseMember/memberList';
     return api_request(url, 'POST', param);
  },
  //获取banner
  get_banner: function(){
    var url = '/banner/index';
    return api_request(url)
  },
  //获取商机
  get_announcement: function(){
     var url = 'announcement/index';
     return api_request(url)
  },
  //获取分类
  get_category: function(){
    var url = 'category/index';
    return api_request(url)
  },
  category_list: function(){
    var url = 'category/list';
    return api_request(url)
  },
  //发送验证码
  send_sms: function(param){
     var url = 'sms/send';
     return api_request(url, 'POST', param);
  },
  //登录
  login: function(param){
     var url = 'login/index';
     return api_request(url, 'POST', param)
  },
  get_userInfo: function(param){
    var url = 'login/userinfo';
    return api_request(url, 'POST', param)
  },
  //获取企业信息
  get_enterprise: function(param){
     var url = 'enterprise/getEnterprise'
     return api_request(url, 'POST', param)
  },
  //新增企业信息
  add_enterprise: function(param){
    var url = 'enterprise/add'
    return api_request(url, 'POST', param)
 },
 //获取企业信息
 edit_enterprise: function(param){
  var url = 'enterprise/edit'
  return api_request(url, 'POST', param)
 },
 enterprise_list: function(param){
  var url = 'enterprise/list'
  return api_request(url, 'POST', param) 
 },

 get_member: function(param){
   var url = 'enterpriseMember/getMember';
   return api_request(url, 'POST', param)
 },
 //通讯录
 address_index: function(param){
   var url = 'address/index';
   return api_request(url, 'POST', param)
 },
 //获取会员详情
 address_detail: function(param){
   var url = 'address/detail';
   return api_request(url, 'POST', param)
 },
 //添加需求
 add_supply1: function(param){
   var url = 'supply1/add';
   return api_request(url, 'POST', param)
 },
 edit_supply1: function(param){
  var url = 'supply1/edit';
  return api_request(url, 'POST', param)
},
getSupply1_3: function(param){
  var url = 'business/getSupply1_3';
  return api_request(url, 'POST', param)
},
 //添加服务
 add_supply2: function(param){
  var url = 'supply2/add';
  return api_request(url, 'POST', param)
},
edit_supply2: function(param){
  var url = 'supply2/edit';
  return api_request(url, 'POST', param)
},
//获取商机列表
business: function(param){
  var url = 'business/index';
  return api_request(url, 'POST', param)
},
business_supply1: function(param, page){
  var url = 'business/supply1/page/'+ page;
  return api_request(url, 'POST', param)
},
business_supply2: function(param, page){
  var url = 'business/supply2/page/'+ page;
  return api_request(url, 'POST', param)
},
// 获取企业详情
enterprise_index: function(param){
  var url = 'enterprise/getEnterpriseById';
  return api_request(url, 'POST', param)
},
add_product: function(param){
  var url = 'product/add';
  return api_request(url, 'POST', param)
},

product_index: function(param, page){
  var url = 'product/index/page/'+page;
  return api_request(url, 'POST', param)
},

product_list: function(param){
  var url = 'product/list';
  return api_request(url, 'POST', param)
},

product_show: function(param){
  var url = 'product/show';
  return api_request(url, 'POST', param)
},
product_listp: function(param, page){
  var url = 'product/listp/page/' + page;
  return api_request(url, 'POST', param)
},
product_listp1: function(param, page){
  var url = 'product/listp1/page/' + page;
  return api_request(url, 'POST', param)
},
edit_product: function(parm){
  var url = '/product/edit';
  return api_request(url, 'POST', parm);
},
del_product: function(param){
  var url = '/product/del';
  return api_request(url, 'POST', param);
},

search_index: function(param){
  var url = 'sym/index';
  return api_request(url, 'POST', param)
},

search_del: function(param){
   var url = 'sym/delSearch';
   return api_request(url, 'POST', param)
},

search_list: function(param){
  var url = 'sym/search';
  return api_request(url, 'POST', param)
},
privacy_policy: function(param){
  var url = 'privacyPolicy/index';
  return api_request(url, 'POST', param)
},

communicate_add: function(param){
  var url = 'communicate/add';
  return api_request(url, 'POST', param)
},
communicate_index: function(param, page){
  var url = 'communicate/index/page/'+ page;
  return api_request(url, 'POST', param)
},

equity_index: function(param){
  var url = 'equity/index';
  return api_request(url, 'POST', param)
},

enterprise_list_page:function(param, page){
  var url = 'enterprise/getListPage/page/'+page;
  return api_request(url, 'POST', param)
},

address_list_page:function(param, page){
  var url = 'address/listP/page/'+page;
  return api_request(url, 'POST', param)
},


my_supply1:function(param, page){
  var url = 'supply1/index/page/'+page;
  return api_request(url, 'POST', param)
},


my_supply2:function(param, page){
  var url = 'supply2/index/page/'+page;
  return api_request(url, 'POST', param)
},

del_supply1: function(param){
  var url = 'supply1/del';
  return api_request(url, 'POST', param)
},

del_supply2: function(param){
  var url = 'supply2/del';
  return api_request(url, 'POST', param)
},

memberSupply1_index: function(param, page){
  var url = 'MemberSupply1/index/page/'+ page;
  return api_request(url, 'POST', param)
},
memberSupply1_match: function(param){
  var url = 'Supply1/match';
  return api_request(url, 'POST', param)
},
memberSupply1_allread: function(param){
  var url = 'MemberSupply1/allRead';
  return api_request(url, 'POST', param)
},

memberSupply1_read: function(param){
  var url = 'MemberSupply1/read';
  return api_request(url, 'POST', param)
},

memberSupply1_del: function(param){
  var url = 'MemberSupply1/del';
  return api_request(url, 'POST', param)
},
memberSupply1_count: function(param){
  var url = 'MemberSupply1/count';
  return api_request(url, 'POST', param)
},

supply1_one: function(param){
  var url = 'supply1/getOne';
  return api_request(url, 'POST', param)
},

supply2_one: function(param){
  var url = 'supply2/getOne';
  return api_request(url, 'POST', param)
}


}